<template>
	<div class="pages">
		<el-form :inline="true" size="small" :model="dataForm" label-width="80px">
			<el-form-item v-if="$hasPermission('api/corp/add')">
				<el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
			</el-form-item>
			<el-form-item label="企业名称:">
				<el-input v-model="dataForm.name" placeholder="请输入" clearable></el-input>
			</el-form-item>
			<el-form-item label="登录账号:">
				<el-input v-model="dataForm.username" placeholder="请输入" clearable></el-input>
			</el-form-item>
			<el-form-item label="所在地区:">
				<el-cascader :options="regionData" v-model="addressArr" @change="handleChange" placeholder="请选择省市区"
					style="width: 100%;" clearable></el-cascader>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="getDataList">搜索</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="dataList" v-loading="dataListLoading" border style="width: 100%" header-align="center">
			<el-table-column type="index" align="center" label="序号" width="60">
			</el-table-column>
			<el-table-column prop="name" align="center" label="企业名称">
			</el-table-column>
			<el-table-column prop="card" align="center" label="统一社会信用代码">
			</el-table-column>
			<el-table-column prop="provinceId" align="center" label="所在地区">
					<template slot-scope="scope">
						<span>{{scope.row.provinceName+scope.row.cityName+scope.row.areaName}}</span>
					</template>
			</el-table-column>
			<el-table-column prop="address" align="center" label="企业实际住所">
			</el-table-column>
			<el-table-column prop="username" align="center" label="登录账号">
			</el-table-column>
			<el-table-column prop="create_time" align="center" label="创建时间">
			</el-table-column>
			<el-table-column align="center" label="状态">
				<template slot-scope="scope">
					<el-dropdown @command="handleCommand($event,scope.row.id)">
						<el-button :type="scope.row.status=='1'?'primary':'danger'" size="mini">
							{{scope.row.status==1?'正常':'禁用'}}<i class="el-icon-arrow-down el-icon--right"></i>
						</el-button>
						<el-dropdown-menu slot="dropdown">
							<el-dropdown-item command="1">正常</el-dropdown-item>
							<el-dropdown-item command="0">禁用</el-dropdown-item>
						</el-dropdown-menu>
					</el-dropdown>
				</template>
			</el-table-column>
			<el-table-column prop="remarks" align="center" label="备注">
			</el-table-column>
			<el-table-column align="center" label="操作" width="160">
				<template slot-scope="scope">
					<el-button type="text" @click="showDetail(scope.row)" v-if="$hasPermission('api/corp/detail')">查看详情</el-button>
					<el-button type="text" @click="addOrUpdateHandle(scope.row.id)" v-if="$hasPermission('api/corp/edit')">编辑</el-button>
					<el-button type="text" @click="deleteHandle(scope.row.id)" v-if="$hasPermission('api/corp/delete')">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
			layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
			@current-change="pageCurrentChangeHandle">
		</el-pagination>
		<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
	</div>
</template>

<script>
	import mixinViewModule from '@/mixins/view-module'
	import AddOrUpdate from './add-or-update'
	import {
		provinceAndCityData,
		regionData,
		rovinceAndCityDataPlus,
		regionDataPlus,
		CodeToText,
		TextToCode
	} from "element-china-area-data"
	export default {
		mixins: [mixinViewModule],
		data() {
			return {
				regionData,
				TextToCode,
				CodeToText,
				mixinViewModuleOptions: {
					getDataListURL: '/api/corp/getlist',
					getDataListIsPage: true,
					deleteURL: '/api/corp/delete',
					deleteIsBatch: true,
					createdIsNeed: true,
				},
				dataForm: {
					name: '',
					username: '',
					provinceId: '',
					cityId:'',
					areaId:''
				},
				addressArr: [],
			}
		},
		components: {
			AddOrUpdate
		},
		methods: {
			showDetail(row) {
				this.$router.push({
					path: '/company/detail',
					query: {
						id: row.id
					}
				})
			},
			handleCommand(command, id) {
				if(!this.$hasPermission('api/corp/setAllStatus')){
					return this.$message.warning("暂无权限！")
				}
				this.$loading()
				this.$http.post('/api/corp/setAllStatus', {
					ids: id,
					status: command
				}).then(res => {
					this.$message.success(res.msg)
					this.$closeLoading()
					this.getDataList()
				})
			},
			handleChange(value) {
				if (value) {
					this.dataForm.provinceId = value[0]
					this.dataForm.cityId = value[1]
					this.dataForm.areaId = value[2]
				}
			},
			formatter(row) {
				return CodeToText[row.provinceId]+CodeToText[row.cityId]+CodeToText[row.areaId]
			},
		},
		created() {
		}
	}
</script>